home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / parser / par_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  9.0 KB  |  501 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <tree.h>
  4. # include    "parser.h"
  5. # include    <catalog.h>
  6. # include    <pv.h>
  7. # include    <symbol.h>
  8. # include    <sccs.h>
  9. # include     "scanner.h"
  10. # include    <errors.h>
  11.  
  12. SCCSID(@(#)par_util.c    8.2    2/14/85)
  13.  
  14. unsigned int Patspec_flag[PATNUM];
  15.  
  16. /*
  17. **  PAR_UTIL -- parser utility functions
  18. **
  19. **    These functions are generally unrelated except that they are
  20. **    needed to operate the parser and are too small to be considered
  21. **    seperate modules.
  22. **
  23. **    Defined Constants:
  24. **
  25. **    Defines:
  26. **        timeofday    -- convert arguments to minutes since midnight
  27. **        tlprepend    -- attach two target list components
  28. **        header        -- prints the header for a retrieve to terminal
  29. **        patmat        -- converts pattern matching characters in a string
  30. **        permcom        -- adds a command to the permit command vector
  31. **
  32. **    Requires:
  33. **        nothing
  34. **
  35. **    Required By:
  36. **        y.tab.c        -- the grammar
  37. **
  38. **    Files:
  39. **        none
  40. **
  41. **    Compilation Flags:
  42. **        none
  43. **
  44. **    Trace Flags:
  45. **        PAR_UTIL.C ~~ 62, 63
  46. **
  47. **    History:
  48. **        20 Dec 1978    -- written (rick)
  49. */
  50.  
  51. /*
  52. **  TIMEOFDAY -- convert 2 integers to minutes since midnight
  53. **
  54. **    Converts the hours and minutes parameters to minutes since midnight
  55. **    performing some error (bounds) checking on the time.
  56. **
  57. **    To answer the question about what is midnight, both 0:00 and 24:00
  58. **    are handled, but not the same way.  The former is zero minutes from
  59. **    midnight and the latter is 1440 minutes from midnight.  (1440 is
  60. **    24 hrs times 60 minutes, or 1 minute past the end of the day.)
  61. **
  62. **    Parameters:
  63. **        hrs        -- an integer pointer to the hour
  64. **        mins        -- an integer pointer to the minutes
  65. **
  66. **    Returns:
  67. **        integer time since midnight
  68. **
  69. **    Side Effects:
  70. **        may detect an error and call par_error which never returns.
  71. **
  72. **    Requires:
  73. **        that the pointers be on integer boundaries
  74. **
  75. **    Called By:
  76. **        y.tab.c        -- the grammar
  77. **
  78. **    Trace Flags:
  79. **        none
  80. **
  81. **    Diagnostics:
  82. **        BADHOURS    -- No such hour
  83. **        BADMINS        -- No such minute
  84. **        BAD24TIME    -- only 24:00 allowed
  85. **
  86. **    Syserrs:
  87. **        none
  88. **
  89. **    History:
  90. **        20 Dec 1978     -- written (rick)
  91. */
  92. timeofday(hrs, mins)
  93. short    *hrs;
  94. short    *mins;
  95. {
  96.     register int    h;
  97.     register int    m;
  98.     register int    rtval;
  99.  
  100.     h = *hrs;
  101.     m = *mins;
  102.     if (h > 24 || h < 0)
  103.         /* no such hour */
  104.         par_error(BADHOURS, WARN, iocv(h), 0);
  105.     if (m > 59 || m < 0)
  106.         /* no such minute */
  107.         par_error(BADMINS, WARN, iocv(m), 0);
  108.     if (h == 24)
  109.     {
  110.         h = 1440;
  111.         if (m != 0)
  112.             /* can only use 24:00 */
  113.             par_error(BAD24TIME, WARN, iocv(m), 0);
  114.     }
  115.     rtval = (h * 60) + m;
  116.     return (rtval);
  117. }
  118.  
  119.  
  120. /*
  121. **  TLPREPEND -- combine two target list components
  122. **
  123. **    Attach two target list components to each other.
  124. **    Neither component need be a single element.  The
  125. **    'a' component will be attached at the extreme left
  126. **    of the 'b' component.
  127. **
  128. **    Parameters:
  129. **        a        -- tl component to attach
  130. **        b        -- tl base for attaching
  131. **
  132. **    Returns:
  133. **        nothing
  134. **
  135. **    Side Effects:
  136. **        this routine is a side effect.  It attaches a to b
  137. **        and when it returns a is attached to b but the pointer
  138. **        to b never changes (neither does the pointer to a)
  139. **
  140. **    Requires:
  141. **        nothing
  142. **
  143. **    Called By:
  144. **        y.tab.c        -- the grammar
  145. **
  146. **    Trace Flags:
  147. **        tlprepend ~~ 62.4
  148. **
  149. **    Diagnostics:
  150. **        none
  151. **
  152. **    Syserrs:
  153. **        none
  154. **
  155. **    History:
  156. **        20 Dec 1978    -- written (rick)
  157. */
  158.  
  159. QTREE *
  160. tlprepend(a, b)
  161. QTREE    *a;
  162. QTREE    *b;
  163. {
  164.     register QTREE    *q;
  165.  
  166. # ifdef    xPTR1
  167.     tTfp(62, 4, "tlprepend\n");
  168. # endif
  169.  
  170.     if (b==NULL)
  171.         return(a);
  172.     /* scan to the left end of b */
  173.     for (q = b; q->left != NULL; q = q->left)
  174.         ;    /* no action */
  175.     
  176.     /* attach a to the end of b */
  177.     q->left = a;
  178.     return (b);
  179. }
  180.  
  181.  
  182.  
  183. /*
  184. **  HEADER.C -- print header for retrieve to terminal
  185. **
  186. **    "setp" to reconstruct the field names and types and passing
  187. **    them to the normal printhdr etc.
  188. **
  189. **    Defines:
  190. **        header()
  191. **
  192. **    Requires:
  193. **        printhdr    - utility lib
  194. **        beginhdr    - utility lib
  195. **        printeol    - utility lib
  196. **        printeh        - utility lib
  197. **        atoi        - utility lib
  198. **        Dc        - vble, number of params in list
  199. **        Dv        - vble, list of parameters
  200. **
  201. **    Trace Flags:
  202. **        none
  203. **
  204. **    History:
  205. **        written (ancient history) (rick)
  206. */
  207. header(pv)
  208. PARM    *pv;
  209. {
  210.     int        len;
  211.     HDRINFO        *hptr;
  212.     HDRINFO        *tptr;
  213.     int        start = 1;
  214.     extern HDRINFO    *Hdrptr;
  215.     extern HDRINFO    *Fieldwidth;
  216.     extern int    Hdr;
  217.  
  218.     Hdr = TRUE;
  219.     beginhdr();
  220.  
  221.  
  222.     for (; pv->pv_type != PV_EOF; pv += 2)
  223.     {
  224.         if ((pv[1].pv_val.pv_str[0] & I1MASK) == 'c')
  225.         {
  226.             tptr = (HDRINFO *) malloc(sizeof(HDRINFO));
  227.             if (start)
  228.             {
  229.                 Hdrptr = tptr;
  230.                 Fieldwidth = Hdrptr;
  231.                 start = 0;
  232.             }
  233.             else
  234.                 hptr->next = tptr;
  235.             hptr = tptr;
  236.         }
  237.  
  238.          len = atoi(&pv[1].pv_val.pv_str[1]);
  239.         printhdr(pv[1].pv_val.pv_str[0] & I1MASK, len, pv->pv_val.pv_str);
  240.  
  241.         if ((pv[1].pv_val.pv_str[0] & I1MASK) == 'c')
  242.         {
  243.             tptr->len = len;
  244.             tptr->len &= 0377;
  245.             tptr->next = NULL;
  246.         }
  247.     }
  248.     printeol();
  249.     printeh();
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. /*
  258. **  PATMAT -- converts pattern matching characters in a string
  259. **
  260. **    Searches a string up to a null byte for one of the pattern
  261. **    matching characters '*', '?', '[', and ']'. It then converts
  262. **    these characters to their internal control character equivalents.
  263. **
  264. **    Parameters:
  265. **        str        -- the string to search
  266. **
  267. **    Returns:
  268. **        0        -- always
  269. **
  270. **    Side Effects:
  271. **        none
  272. **
  273. **    Requires:
  274. **        symbol.h
  275. **
  276. **    Called By:
  277. **        y.tab.c        -- grammar
  278. **
  279. **    Trace Flags:
  280. **        none
  281. **
  282. **    Diagnostics:
  283. **        none
  284. **
  285. **    Syserrs:
  286. **        none
  287. **
  288. **    History:
  289. **        written (ancient history) (rick)
  290. **        amended (Heidi) -- checks for numbers after PAT_SPEC's 
  291. **            and does away with flags making pattern matching
  292. **            characters legal in strings
  293. */
  294.  
  295.  
  296. /*
  297. ** PATMAT
  298. **    hunts through a string and converts the pattern matching
  299. **    characters and replaces with the corresponding cntrl chars
  300. */
  301. patmat(str)
  302. char    *str;
  303. {
  304.     register int    i;        /* index variables */
  305.     register char    *p, *q, c;
  306.     extern int    Qlflag;
  307.  
  308.     q = str;
  309.     for (p = str; *p; p++)
  310.     {    
  311.         if (*p == '\\')
  312.         {
  313.             *q++ = *++p;
  314.             continue;
  315.         }
  316.         switch (*p)
  317.         {
  318.                 case '#':
  319.                   if (*(p + 1) == '#') 
  320.                   {
  321.                       p++;
  322.                       if ((c = *(p + 1)) == '0')
  323.                       {
  324.                           *q++ = PAT_GLOB;
  325.                           p++;
  326.                       }
  327.                       else if (c >= '1' && c <= '9')
  328.                 {
  329.                     if ( !Qlflag )             /* target*/
  330.                         Patspec_flag[c - '0'] = 1;
  331.                     else             /* qualifier */
  332.                     {
  333.                         if( Patspec_flag[c - '0'] == (TARGBIT | QUALBIT))
  334.                         {
  335.                         for (i=0; i<PATNUM; i++)
  336.                             Patspec_flag[i] = 0;
  337.                         par_error(DUPINDEX,WARN,0);
  338.                         }
  339.                         /* allows for repeated indices in
  340.                         ** qualifier as long as index is not
  341.                         ** mentioned in target
  342.                         */
  343.                         else
  344.                         Patspec_flag[c - '0'] = Patspec_flag[c - '0'] | QUALBIT;
  345.                     }
  346.                           *q++ = PAT_SPEC;
  347.                 }
  348.                       else 
  349.                           par_error(NOINDEX,WARN,0);
  350.                       continue;
  351.                   }
  352.                   else
  353.                       *q++ = *p;
  354.                   continue;
  355.       
  356.                 case '*':
  357.             if (!Qlflag)
  358.                 par_error(NOPATMAT, WARN, 0);
  359.                   *q++ = PAT_ANY;
  360.                   continue;
  361.  
  362.           case '?':
  363.             if (!Qlflag)
  364.                 par_error(NOPATMAT, WARN, 0);
  365.             *q++ = PAT_ONE;
  366.             continue;
  367.  
  368.           case '[':
  369.             if (!Qlflag)
  370.                 par_error(NOPATMAT, WARN, 0);
  371.             *q++ = PAT_LBRAC;
  372.             continue;
  373.  
  374.           case ']':
  375.             if (!Qlflag)
  376.                 par_error(NOPATMAT, WARN, 0);
  377.             *q++ = PAT_RBRAC;
  378.             continue;
  379.  
  380.           default:
  381.             *q++ = *p;
  382.             continue;
  383.         }
  384.     }
  385.     *q = '\0';
  386.     return (0);
  387. }
  388. /*
  389. **  PERMCOM -- map command allowed into protection catalog bits
  390. **
  391. **    translates the QMODE type symbols into the appropriate counterparts
  392. **    for the permit statement allowed command vector.  The manifest
  393. **    constants are designed to be inclusive or'd together to form a
  394. **    composite bit map of OK actions.
  395. **
  396. **    Parameters:
  397. **        a        -- the QMODE type symbol for the command to add
  398. **
  399. **    Returns:
  400. **        none
  401. **
  402. **    Side Effects:
  403. **        changes the variable Permcomd to reflect the additional permission
  404. **
  405. **    Requires:
  406. **        Permcomd must be define globally
  407. **        catalog.h for the proper constants
  408. **
  409. **    Called By:
  410. **        y.tab.c        -- the grammar
  411. **
  412. **    Trace Flags:
  413. **        none
  414. **
  415. **    Diagnostics:
  416. **        none
  417. **
  418. **    Syserrs:
  419. **        bad QMODE(%d)    -- a bad symbol has been passed for mapping
  420. **
  421. **    History:
  422. **        28 Dec 1978    -- written (rick)
  423. */
  424.  
  425. permcom(a)
  426. int    a;
  427. {
  428.     extern int        Permcomd;
  429.     switch (a)
  430.     {
  431.       case mdRETR:
  432.         Permcomd |= PRO_RETR;
  433.         break;
  434.  
  435.       case mdAPP:
  436.         Permcomd |= PRO_APP;
  437.         break;
  438.     
  439.       case mdREPL:
  440.         Permcomd |= PRO_REPL;
  441.         break;
  442.  
  443.       case mdDEL:
  444.         Permcomd |= PRO_DEL;
  445.         break;
  446.  
  447.       case -1:
  448.         Permcomd |= 0177777;        /* all bits set */
  449.         break;
  450.     
  451.       default:
  452.         syserr("permcom: bad QMODE(%d)", a);
  453.     }
  454. }
  455.  
  456. char    *
  457. makestr(str)
  458. register char    *str;
  459. {
  460.     register char    *result;
  461.     register int    len;
  462.  
  463.     len = length(str) + 1;
  464.  
  465.     result = (char *) need(Qbuf, len);
  466.  
  467.     bmove(str, result, len);
  468.  
  469.     return (result);
  470. }
  471.  
  472.  
  473. /*
  474. **    QUALINDEX -- 
  475. **        check to see if a PAT_SPEC index was used in a target
  476. **        list and not in a qualifier list
  477. **
  478. **        Returns: 0 if ok
  479. **             calls par_error if not ok
  480. **
  481. **        Called by: y.tab.c   -grammar
  482. */
  483.  
  484. qualindex()
  485. {
  486.     int i;
  487.  
  488.     for (i=0; i<PATNUM; i++)
  489.     {
  490.         if (Patspec_flag[i] == TARGBIT)
  491.         {
  492.             /* reset the rest of the flag array */
  493.             while (i++ < PATNUM)
  494.                  Patspec_flag[i] = 0;
  495.             par_error(NOQUALINDX, WARN, 0);
  496.         }
  497.         Patspec_flag[i] = 0;
  498.     }
  499.     return(0);
  500. }
  501.